<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ul li {
            list-style: none;
            line-height: 40px;
        }
    </style>
</head>
<!-- v-for 案例 -->

<body>
    <div id="app">
    
        <div>
            <h3>添加数据</h3>
            <div>
                <label for="id">ID：</label>
                <input id="id" type="text" v-model="id" />
            </div>
            <div>
                <label for="name">name：</label>
                <input id="name" type="text" v-model="name" />
            </div>
        </div>

        <div>
            <button @click="add">添加</button>
        </div>

        <div>
            <h3>数据展示</h3>
            <ul>
                <li v-for="item in listArr" :key="item.id">
                    <input type="checkbox">
                    {{item.id}}  --  {{item.name}}
                </li>
            </ul>
        </div>

    </div>
    <script src="js/vue.js"></script>

    <script>
        const HelloVue = {
            data() {
                return {
                    id:'',
                    name:'',
                    listArr:[
                        {id:'001', name:'张三'},
                        {id:'002', name:'李四'},
                        {id:'003', name:'王五'},
                        {id:'004', name:'赵六'},
                        {id:'005', name:'叶琪'},
                        {id:'006', name:'李玉'},
                        {id:'007', name:'厚诚'},
                    ]
                }
            },
            computed: {
                
            },
            methods: {
                add(){
                    // 尾部追加
                    // this.listArr.push({id: this.id, name: this.name});

                    // 头部追加
                    // 不绑定 key 的时候： 如果是在页面上面勾选了第一个，那么添加上去的数据会被勾选住，而不是原来的那个勾选住
                    this.listArr.unshift({id: this.id, name: this.name});   


                    // 中间追加（也会出现，头部追加的问题）
                    // this.listArr.splice(2, 0, {id: this.id, name: this.name});
                }
            }
        };

        const app = Vue.createApp(HelloVue).mount("#app");
    </script>
</body>

</html>